#include <iostream>
#include <string>
#include "logtxt.hpp"
#include "Init.hpp"
using namespace std;
Log lg;
Init init;
enum
{
    DIVERR = 1,
    MODELERR,
    OPERR
};

class Task
{
private:
    int sockfd_;
    string clientip_;
    uint16_t clientport_;

public:
    Task(int sockfd, const string &clientip, const uint16_t &clientport) : sockfd_(sockfd), clientip_(clientip), clientport_(clientport)
    {
    }
    Task()
    {
    }
    ~Task() {}
    void run()
    {
        char buffer[1024];
        ssize_t s = read(sockfd_, buffer, sizeof(buffer));
        if (s > 0)
        {
            buffer[s] = 0;
            cout << "get a client message:" << buffer << endl;
            string message = "server sendto message:";
            message += init.tranlation(buffer);
            write(sockfd_, message.c_str(), message.size());
            close(sockfd_);
        }
        else if (s == 0)
        {
            lg(Info, "%s%d is quit,server close socket:%d", clientip_.c_str(), clientport_, sockfd_);
        }
        else
        {
            cerr << "read fail" << endl;
        }
        close(sockfd_);
    }
};